package controllers

import (
	"github.com/astaxie/beego"
	"shuxianghui/models"
	"HTshuxianghui/htmodels"
	"strconv"
	"github.com/astaxie/beego/validation"
	"time"
)

type CityController struct {
	beego.Controller
}

//带了参数导致bootstrap table无法正常使用
func (c *CityController)HTCityList_html() {
	c.TplName = "city_list.html"
	c.Render()
}

func (c *CityController)HTCityList() {
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "CityName"}
	City := []models.City{}
	htmodels.HTCityGetList(&City, fileds, offset, limit, search, "CityName", orderby)
	count, _ := htmodels.HTCityGetCount(search, "CityName", )
	c.Data["json"] = map[string]interface{}{"rows":&City, "total":count}
	c.ServeJSON()
}

func (c *CityController)HTCityDetail() {
	Id, _ := c.GetInt64("Id")
	if Id != 0 {
		c.Data["update"] = true
		SetCityDetailInfo(c, Id)
	}
	mytype, _ := c.GetInt("type")
	c.Data["Type"] = mytype
	c.TplName = "city_detail.html"
}

func SetCityDetailInfo(c *CityController, Id int64) {
	City := models.City{Id:int(Id)}
	htmodels.HTCityGetDetail(&City)
	c.Data["City"] = &City
}

func (c *CityController)HTCityAdd() {
	City := models.City{}
	if err := c.ParseForm(&City); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	valide := validation.Validation{}
	if b, err := valide.Valid(&City); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTCityAdd(&City); err != nil {
		Message(&c.Controller, "", "添加失败，即将返回！" + err.Error(), "", true)
	}
	user := models.Users{Username:City.CityName + "管理员", UserType:10, CityId:City.Id, CreateTime:time.Now(), UpdateTime:time.Now(),Phone:strconv.Itoa(City.Id) + "@admin.com", Email:strconv.Itoa(City.Id) + "@admin.com", Password:GetPassword("123456")}
	if err := htmodels.HTUsersAdd(&user); err != nil {
		Message(&c.Controller, "", "添加地区成功，添加管理员失败，即将返回！" + err.Error(), "", true)
	}
	dushuhui:=models.Article{ArticleType:3,CityId:City.Id,CreateTime:time.Now(),EditTime:time.Now(),Content:"这里是"+City.CityName+"读书会简介",Introduction:"这里是"+City.CityName+"读书会简介"}
	if err:=htmodels.HTArticleAdd(&dushuhui);err!=nil{
		Message(&c.Controller, "", "添加地区成功，添加读书会简介失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/city/detail`, "添加成功！", "", false)
}

func (c *CityController)HTCityUpdate() {
	Id, _ := c.GetInt64("Id");
	City := models.City{Id:int(Id)}
	if err := htmodels.HTCityGetDetail(&City); err != nil {
		Message(&c.Controller, "", "严重错误，请联系管理员,即将返回！" + err.Error(), "", true)
	}
	if City.Virtural==1 {
		Message(&c.Controller, "", "虚拟读书会不可修改！", "", true)
	}
	if err := c.ParseForm(&City); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	valide := validation.Validation{}
	if b, err := valide.Valid(&City); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTCityUpdate(&City); err != nil {
		Message(&c.Controller, "", "更新失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/city/list_html`, "更新成功！", "", false)
}

func (c *CityController)HTCityDelete() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	count, err := htmodels.HTCityDelete(Ids)
	if err != nil {
		Json_Message(&c.Controller, 0, "删除失败" + err.Error())
	}
	for _, id := range Ids {
		m := models.Users{UserType:10, CityId:int(id)}
		htmodels.HTUserGuanliDelete(&m)
	}
	Json_Message(&c.Controller, 1, "删除" + strconv.FormatInt(count, 10) + "行")
}

//http://localhost:8083/city/list?order=asc&offset=0&limit=500